var haveCart = false;
$(function () {
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var jsonstr = "{token:" + token + "}";
    var url = "/except/shopcontroller/shopcart.action";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            var addressinfo = "";
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                var datas = data.data.shopcart;
                var usertype = data.data.usertype;
                $(datas).each(function () {
                    var price = 0;
                    if(usertype * 1 == 1)
                        price = this.first;
                    else if(usertype * 1 == 2)
                        price = this.second;
                    else if(usertype * 1 == 3)
                        price = this.third;
                    else if(usertype * 1 == 4)
                        price = this.forth;
                    else if(usertype * 1 == 5)
                        price = this.fifth;
                    else
                        price = this.price;
                    $("#shopcart").append('<div id="div' + this.shopcart_id + '" class="item clearboth" onclick="chose_radio(\'' + this.shopcart_id + '\', true);">' +
                        '<div class="left">' +
                        '<input type="radio" id="' + this.shopcart_id + '" class="radio_left" />' +
                        '<input type="hidden" id="shopid' + this.shopcart_id + '" value="' + this.shop_id + '"/>' +
                        '<input type="hidden" id="age' + this.shopcart_id + '" value="' + this.bady_age_id + '"/>' +
                        '<input type="hidden" id="sex' + this.shopcart_id + '"  value="' + this.bady_sex + '"/>' +
                        '<input type="hidden" id="specid' + this.shopcart_id + '" value="' + this.shop_spec_price + '"/>' +
                        '<input type="hidden" id="shop_img' + this.shopcart_id + '" value="' + this.img + '"/>' +
                        '</div>' +
                        '<div class="right">' +
                        '<div class="goods">' +
                        // '<img src="' + this.img + '" id="shop_img' + this.shopcart_id + '" class="item_img"  onerror="imgerror(this,\'img/onerror.png\');"/>' +

                        '<div style="float: left;margin-right: 10px; width:85px; height:85px;overflow-y: hidden; ' +
                        'background: url('+this.img+') no-repeat center;background-size: cover;"' +
                        ' onerror="imgerror(this,\'img/onerror.png\');"></div>'+

                        '<ul class="item_ul">' +
                        '<li><span class="item_name" id="shop_name' + this.shopcart_id + '">' + this.shop_name + '</span></li>' +
                        '<li>' +
                        '<div style="margin-top: 10px;">' +
                        '<span class="item_desc" id="shop_spec_name">' + this.name + '</span>' +


                        '<div class="count">'+
                        '<div class="add_div">'+
                        '<div onclick="minus(\'' + this.shopcart_id + '\')">'+
                        '<span>-</span>'+
                        '</div>'+
                        '<div>'+
                        '<span id="number' + this.shopcart_id + '">' + this.num + '</span>'+
                        '</div>'+
                        '<div onclick="add(\'' + this.shopcart_id + '\')">'+
                        '<span>+</span>'+
                        '</div>'+
                        '</div>'+
                        '</div>'+
                        // '<div class="count">' +
                        // '<b onclick="minus(\'' + this.shopcart_id + '\')">-</b><input type="number" min="1" id="number' + this.shopcart_id + '" value="' + this.num + '" style="width: 20px;margin-left: 10px;"/><b onclick="add(\'' + this.shopcart_id + '\')">+</b>' +
                        // '</div>' +
                        '</div>' +
                        '</li>' +
                        '<li><span class="item_money">￥:<span id="price' + this.shopcart_id + '" >' + price + '</span></span></li>' +
                        '</ul>' +
                        '</div>' +
                        '</div>' +
                        '</div>' +
                        '<hr style="height: 1px; margin-left: 33px; background-color: #f4f4f4; border: none;" />');
                });
                remove_advertising();
            } else if (code == 20013){
                $("#not_data").show();
                haveCart = true;
            }else{
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
        }
    });
});

function modify_save() {
    var modify_save = $("#modify_save").html();
    if (modify_save == "编辑") {
        if(haveCart) {
            $("#hint_message").html("购物车没有商品，不能操作");
            $("#hintbg").show();
        } else {
            $("#modify_save").html("保存");
            $("#right_div").hide();
            $("#calculate_span").html("删除");
        }
    } else {
        $("#modify_save").html("编辑");
        $("#right_div").show();
        $("#calculate_span").html("结算");
        var token = $("#token").val();
        if (isempty(token)) {
            location.href = "/except/login.jsp";
            return;
        }
        var cartinfo = "";
        $("#shopcart :radio").each(function () {
            var checked = $(this).attr("checked");
            if (checked) {
                var cartid = $(this).attr("id")
                cartinfo += "{num:" + $("#number" + cartid).html() + ",shopcartid:" + cartid + "},";
            }
        });
        if (isempty(cartinfo)) {
            $("#hint_message").html("保存成功");
            $("#hintbg").show();
            return;
        } else {
            cartinfo = "{cartinfo:[" + cartinfo.substr(0, cartinfo.length - 1) + "]}";
        }
        var jsonstr = "{token:" + token + "}";
        var url = "/except/shopcontroller/editshopcart.action";
        $.ajax({
            url: url,
            type: 'post',
            dataType: "json",
            data: {
                json: jsonstr,
                cartinfo: cartinfo
            },
            async: false,
            success: function (data) {
                var code = data.code;
                var message = data.message;
                if (code == 10001 || code == 10002)
                    location.href = "/except/login.jsp";
                else if (code == 20000) {
                    $("#hint_message").html("保存成功");
                    $("#hintbg").show();
                } else {
                    $("#hint_message").html(message);
                    $("#hintbg").show();
                }
            }
        });
    }
    radio_disabled();
}

/**
 * 全不选
 */
function radio_disabled() {
    if(haveCart) {
        $("#hint_message").html("购物车没有商品，不能操作");
        $("#hintbg").show();
    } else {
        getprice();
        $("#shopcart :radio").attr("checked", false);
    }
}

/**
 * 全选
 */
function select_all(radio, flag) {
    var i = 0;
    if(haveCart) {
        $("#hint_message").html("购物车没有商品，不能操作");
        $("#hintbg").show();
    } else {
        if(flag) {
            $("#shopcart :radio").attr("checked", true);
            $("#shopcart :radio").each(function () {
                i ++;
                $("#div" + $(this).attr("id")).attr("onclick", "chose_radio('" + $(this).attr("id") + "', false);");
            });
            $(radio).attr("checked", true);
            $(radio).attr("onclick", "select_all(this, false)");
        } else {
            $("#shopcart :radio").attr("checked", false);
            $("#shopcart :radio").each(function () {
                $("#div" + $(this).attr("id")).attr("onclick", "chose_radio('" + $(this).attr("id") + "', true);");
            });
            $(radio).attr("checked", false);
            $(radio).attr("onclick", "select_all(this, true)");
        }
        getprice();
    }
}

function minus(shopcartid) {
    $("#" + shopcartid).attr("checked", true);
    var number = $("#number" + shopcartid).html();
    if (number * 1 <= 1)
        $("#number" + shopcartid).html(1);
    else
        $("#number" + shopcartid).html(number * 1 - 1);
    chose_radio(shopcartid);
}

function add(shopcartid) {
    $("#" + shopcartid).attr("checked", true);
    var number = $("#number" + shopcartid).html();
    $("#number" + shopcartid).html(number * 1 + 1);
    chose_radio(shopcartid);
}

function chose_radio(shopcartid, flag) {
    var checked = $("#" + shopcartid).attr("checked");
    $("#" + shopcartid).attr("checked", flag);
    if (flag) {
        $("#div" + shopcartid).attr("onclick", "chose_radio('" + shopcartid + "', false);");
    } else{
        $("#div" + shopcartid).attr("onclick", "chose_radio('" + shopcartid + "', true);");
    }
    getprice();
}

function calculate_del() {
    var calculate = $("#calculate_span").html();
    if (calculate == "删除") {
        $("#message").html("确定删除吗？");
        $("#confirmbg").show();
    } else {
        var cartinfo = "";
        $("#shopcart :radio").each(function () {
            var checked = $(this).attr("checked");
            if (checked) {
                var cartid = $(this).attr("id");
                var img = $("#shop_img" + cartid).val();
                cartinfo += "{num:" + $("#number" + cartid).html() + ",shopcartid:" + cartid + "" +
                    ",shopname:" + $("#shop_name" + cartid).html() + ",shopid:" + $("#shopid" + cartid).val() + "," +
                    "shopspecid:" + $("#specid" + cartid).val() + ",age:" + $("#age" + cartid).val() + "," +
                    "sex:" + $("#sex" + cartid).val() + ",img:\"" + img + "\"," +
                    "specname:"+$("#shop_name" + cartid).html()+",price:"+$("#price" + cartid).html()+"},";
            }
        });
        if (isempty(cartinfo)) {
            $("#hint_message").html("购物车没有商品，不能操作");
            $("#hintbg").show();
            return;
        } else {
            cartinfo = cartinfo.substr(0, cartinfo.length - 1);
            cartinfo = "{shopinfo:[" + cartinfo + "]}";
            $("#shopinfo").val(cartinfo);
            $("#shopcartfrm").attr("action", "/except/files/indent/pre_order.jsp").submit();
            // location.href = "/except/files/indent/pre_order.jsp";
        }
    }
}

function confirm_state(state) {
    $("#confirmbg").hide();
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    if (state == 1) {
        var cartinfo = "";
        $("#shopcart :radio").each(function () {
            var checked = $(this).attr("checked");
            if (checked) {
                var cartid = $(this).attr("id");
                cartinfo += cartid + ",";
            }
        });
        if (isempty(cartinfo)) {
            $("#message").html("删除成功");
            $("#confirmbg").show();
            return;
        }
        cartinfo = cartinfo.substr(0, cartinfo.length - 1);
        var jsonstr = "{token:" + token + ",cartinfo:[" + cartinfo + "]}";
        var url = "/except/shopcontroller/removeshopcart.action";
        $.ajax({
            url: url,
            type: 'post',
            dataType: "json",
            data: {
                json: jsonstr,
            },
            async: false,
            success: function (data) {
                var code = data.code;
                var message = data.message;
                var addressinfo = "";
                if (code == 10001 || code == 10002)
                    location.href = "/except/login.jsp";
                else if (code == 20000) {
                    $("#hint_message").html("删除成功");
                    $("#hintbg").show();
                    $("#shopcart :radio").each(function () {
                        $("#div" + $(this).attr("id")).remove();
                    });
                } else {
                    $("#hint_message").html(message);
                    $("#hintbg").show();
                }
            }
        });
    }
}

function getprice() {
    var total = 0;
    $("#shopcart :radio").each(function () {
        var checked = $(this).attr("checked");
        if (!checked)
            return;
        var id = $(this).attr("id");
        var price = $("#price" + id).html();
        var number = $("#number" + id).html();
        total += price * 1 * number * 1;
    });
    total = returnFloat(total);
    $("#total_money").html(total);
}

function returnFloat(value) {
    var value = Math.round(parseFloat(value) * 100) / 100;
    var xsd = value.toString().split(".");
    if (xsd.length == 1) {
        value = value.toString() + ".00";
        return value;
    }
    if (xsd.length > 1) {
        if (xsd[1].length < 2) {
            value = value.toString() + "0";
        }
        return value;
    }
}